/**
 * 功能:TK_SURPLUS_ORDER Service类
 * 开发人员:chengd
 * 创建时间:2012/10/15 22:29:30
 */
package com.towker.frame.service.basicData;

import org.springframework.stereotype.Service;
import com.towker.core.db.EntityDao;
import com.towker.core.db.BaseService;
import com.towker.frame.model.basicData.Depots;
import com.towker.frame.model.basicData.SurplusOrder;
import com.towker.frame.dao.basicData.SurplusOrderDao;

@Service
public class SurplusOrderService extends BaseService<SurplusOrder, java.lang.Long>
{
	private SurplusOrderDao dao;
	private DepotsService depotsService;
	private GoodsService goodsService;
	
	public void setSurplusOrderDao(SurplusOrderDao dao)
	{
		this.dao = dao;
	}

	public void setDepotsService(DepotsService depotsService) {
		this.depotsService = depotsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao()
	{
		return this.dao;
	}
	
	public double getAmount(int goodsId, String depotCode) {
		SurplusOrder dg = this.dao.getByDepotCodeAndGoodsId(depotCode, goodsId);
		if (dg == null) {
			return 0D;
		} else {
			return 0D;
		}
	}
	
	public void addStockAmount(Integer goodsId, String depotCode, String coCode, double amount, boolean updateGoods) {
		Depots d = depotsService.getByCode(depotCode);
		if (d != null) {
			addStockAmount(goodsId, d.getId(), coCode, amount, updateGoods);
		}
	}
	public void addStockAmount(Integer goodsId, Integer depotId, String coCode, double amount, boolean updateGoods) {
		SurplusOrder dg = this.dao.getByDepotAndGoods(depotId, goodsId, coCode);
		if (dg == null) {
			dg = new SurplusOrder();
			dg.setDepot_id(depotId);
			dg.setGoods_id(goodsId);
			dg.setStock(amount);
			add(dg);
		} else {
			dg.setStock(dg.getStock() + amount);
			update(dg);
		}
		if (updateGoods) {
			goodsService.addStockAmount(goodsId.longValue(), amount);
		}
	}
	public void addDownOrderAmount(Integer goodsId, String depotCode, String coCode, double amount, boolean updateGoods) {
		Depots d = depotsService.getByCode(depotCode);
		if (d != null) {
			addDownOrderAmount(goodsId, d.getId(), coCode, amount, updateGoods);
		}
	}
	public void addDownOrderAmount(Integer goodsId, Integer depotId, String coCode, double amount, boolean updateGoods) {
		SurplusOrder dg = this.dao.getByDepotAndGoods(depotId, goodsId, coCode);
		if (dg == null) {
			dg = new SurplusOrder();
			dg.setDepot_id(depotId);
			dg.setGoods_id(goodsId);
			dg.setDown_order(amount);
			dg.setDown_order_total(amount);
			dg.setCo_code(coCode);
			add(dg);
		} else {
			dg.setDown_order(dg.getDown_order() + amount);
			if (amount > 0) {
				dg.setDown_order_total(dg.getDown_order_total() + amount);
			}
			update(dg);
		}
		if (updateGoods) {
			goodsService.addDownOrderAmount(goodsId.longValue(), amount);
		}
	}
}
